package com.juyu.miliao.api.rabbitMQ;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.HostRecentDevelopmentsMqKey;
import com.juyu.miliao.common.domain.dto.HostRecentDevelopmentsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xuanqicheng
 */
@Component
@Slf4j
public class HostRecentDevelopmentsMQ {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public void setHostRecentDevelopmentsMessage(List<HostRecentDevelopmentsDTO> list) {
        try {
            for(HostRecentDevelopmentsDTO dto : list){
                if (dto != null) {
                    amqpTemplate.convertAndSend(HostRecentDevelopmentsMqKey.EXCHANGE,
                            HostRecentDevelopmentsMqKey.DevelopmentsInfo.ROUTING_KEY,
                            objectMapper.writeValueAsString(dto));
                }
            }

        } catch (Exception ex) {
            log.error("主播最新动态错误");
        }
    }
}
